/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.menu.stateDiagram;


import gui.commands.CommandManager;
import gui.commands.stateDiagramCommands.AddTransitionCommand;
import gui.menu.classDiagram.*;
import data.classDiagram.ClassEntity;
import data.DataVector;
import data.classDiagram.Entity;
import data.classDiagram.Trigger;
import data.stateDiagram.State;
import data.stateDiagram.Transition;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

/**
 *
 * @author SG0217432
 */
public class AddTransitionForm extends JFrame implements ActionListener {

    private JTextField trigger;
    private JTextField condition;
    private JTextField action;
    private JTextField msgs;
    private JComboBox  source;
    private JComboBox  destination;
    
    private JButton    addButton;
    private JButton    cancelButton;

    public AddTransitionForm() {
        super("Add new transition");

        Container contentPane = getContentPane();
        contentPane.setLayout(new GridLayout(0,2));

        contentPane.add(new JLabel("Trigger name "));
        trigger = new JTextField();
        contentPane.add(trigger);
        
        contentPane.add(new JLabel("Condition "));
        condition = new JTextField();
        contentPane.add(condition);
        
        contentPane.add(new JLabel("Action "));
        action = new JTextField();
        contentPane.add(action);
        
        contentPane.add(new JLabel("Message "));
        msgs = new JTextField();
        contentPane.add(msgs);
        
        
        source = new JComboBox();       
        destination = new JComboBox();
        
        for (State e : DataVector.getInstance().getActiveStateDiagram().
                getStateDiagram().getStates()){
                source.addItem(e.getName());
                destination.addItem(e.getName());
        }
    
        contentPane.add(new JLabel("Source "));
        contentPane.add(source);
        
        contentPane.add(new JLabel("Destination "));
        contentPane.add(destination);
        

        addButton = new JButton("Add");
        addButton.addActionListener(this);
        contentPane.add(addButton);

        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);
        contentPane.add(cancelButton);
       
        setVisible(true);
        setSize(getPreferredSize());
    }

    public void actionPerformed(ActionEvent e) {
        JButton button = (JButton) e.getSource();

        if(button == addButton && !trigger.getText().isEmpty()){
	        AddTransitionCommand command = new AddTransitionCommand(
			        condition.getText(), action.getText(),
			        msgs.getText(), trigger.getText(),
			        source.getSelectedItem().toString(),
			        destination.getSelectedItem().toString()
	        );

	        CommandManager.getInstance().addAndExecuteStateDiagramCommand(command);
        }

        setVisible(false);
    }
}
